const App = getApp();
Page({
	data: {
		order_id: 0,
		order_list: {},
		img_list: [],
		img_id: '',
		offline_pay_status:0,
		img_array_id: [],
	},
	DelImage: function(e) {
		var id = e.currentTarget.dataset.id
		var img = e.currentTarget.id
		var img_list = this.data.img_list
		var img_array_id = this.data.img_array_id
		img_list.splice(img_list.indexOf(img), 1)
		img_array_id.splice(img_array_id.indexOf(id), 1)
		this.setData({
			img_list: img_list,
			img_array_id: img_array_id
		})
	},
	radioChange: function(e) {
		this.data.offline_pay_status = e.detail.value
	},
	SubmitOrder: function() {
    console.log(this.data.order_list)
    if(this.data.order_list.is_offline == 0){
      this.data.offline_pay_status = 1
    }
		var order_id = this.data.order_id
		if (!order_id) {
			wx.showModal({
				title: '温馨提示：',
				content: '订单ID有误',
				showCancel: false,
				confirmText: '关闭提示',
				success(res) {

				}
			})
			return false
		}
		var img_array_id = this.data.img_array_id
		if (img_array_id.length < 1) {
			wx.showModal({
				title: '温馨提示：',
				content: '请上传图片',
				showCancel: false,
				confirmText: '关闭提示',
				success(res) {

				}
			})
			return false
		}
		var img_id = "";
		for (var i = 0; i < img_array_id.length; i++) {
			img_id = (i == 0 ? img_array_id[i] : (img_id + ',' + img_array_id[i]));
		}
		var image_ids = img_id

		var offline_pay_status = this.data.offline_pay_status
    console.log(offline_pay_status)
		let _this = this;
		App._post_form('service/uploadCertificate', {
			order_id: order_id,
			image_ids: image_ids,
			offline_pay_status: offline_pay_status
		}, function(result) {
			wx.showModal({
				title: '温馨提示：',
				content: result.msg,
				showCancel: false,
				confirmText: '关闭提示',
				success(res) {
					wx.navigateBack();
				}
			})
		});
	},
	uploadImage: function() {
		var order_id = this.data.order_id
		var that = this
		wx.chooseImage({
			count: 1,
			success(res) {
				const tempFilePaths = res.tempFilePaths
				wx.uploadFile({
					url: App.api_root + 'upload/image',
					filePath: tempFilePaths[0],
					name: 'iFile',
					formData: {
						order_id: order_id,
						image_ids: tempFilePaths[0],
						wxapp_id: App.getWxappId(),
						token: App.getToken()
					},
					success(res) {
						var json = JSON.parse(res.data)
						var img = json.data.file_path
						var id = json.data.file_id
						var img_list = that.data.img_list
						var img_array_id = that.data.img_array_id
						img_list.push(img)
						img_array_id.push(id)
						that.setData({
							img_list: img_list,
							img_array_id: img_array_id
						})
					}
				})
			}
		})
	},
	onLoad: function(options) {
		var order_id = options.order_id
		this.setData({
			order_id: order_id
		})
		this.getOrder()
	},
	getOrder: function() {
		var order_id = this.data.order_id
		let _this = this;
		App._post_form('service/getOrderinfo', {
			order_id: order_id
		}, function(result) {
			_this.setData({
				order_list: result.data.order,
			})
		});
	},
	
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
